define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'dts/student/index' + location.search,
                    add_url: 'dts/student/add',
                    edit_url: 'dts/student/edit',
                    del_url: 'dts/student/del',
                    multi_url: 'dts/student/multi',
                    import_url: 'dts/student/import',
                    table: 'dts_student',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'grade.name', title: __('Grade_id'), operate: 'LIKE'},
                        {field: 'classes.name', title: __('Classes_id'), operate: 'LIKE'},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'gender', title: __('Gender'), searchList: {1: __('Male'), 0: __('Female')}, formatter: Table.api.formatter.normal},
                        {field: 'birthday', title: __('Birthday'), operate: false, addclass:'datetimerange', autocomplete:false},
                        {field: 'relationship', title: __('Relationship'), searchList: {"爸爸":__('爸爸'),"妈妈":__('妈妈'),"爷爷":__('爷爷'),"奶奶":__('奶奶'),"外公":__('外公'),"外婆":__('外婆')}, formatter: Table.api.formatter.normal},
                        {field: 'user_mobile', title: __('User_mobile'), operate: 'LIKE'},
                        {field: 'createby', title: __('Createby'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        moban: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };

    $(".btn-moban").click(function() {
        Fast.api.open('dts/student/moban' + location.search, '导入数据');
    });

    // 动态关联查询条件
    $(document).on('input change', '#c-grade_id', function () {
        var id = $('#c-grade_id').val();
        $('#c-classes_id_text').data('selectPageObject').option.params = function () { return { "custom[grade_id]": id }; };
        // 清空关联查询旧值
        $('#c-classes_id').val('');
        $('#c-classes_id_text').val('');
    });

    return Controller;
});
